package com.wh.wisdomsite.xbox.server.dbCompare;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/***
 * 功能描述：数据库比对功能可以比对不存在的表，不存在的字段，以及不同的长度，注释
 * @Package com.wh.wisdomsite.xbox.server.dbCompare
 * @author 谢泽鹏
 * @date 2016年11月07日 下午15:10
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 * @version V1.0
 */
@Controller
public class DBCompareController {

    private static final Log logger = LogFactory.getLog(DBCompareController.class);



    @RequestMapping(value = "/compareDB.do", method = RequestMethod.GET)
    public String compareDB() {
        return "/compareDB";
    }

    @RequestMapping(value = "compare.do", method = RequestMethod.POST)
    public void compare(HttpServletResponse response, String dbLink, String owner,String owner2, String ip) {

        try {
            CompareCertainDB compCertDB=new CompareCertainDB();
            //不存在的表名集合
            List tableNotInList=compCertDB.tabShldDel(dbLink, owner, owner2, DBConnection.getConn(ip, owner));
            //表明存在，但是字段不存在的字段集合
            List columnsNotInList=compCertDB.compColsInOneNotInOther(dbLink, owner, owner2, DBConnection.getConn(ip, owner));
            //字段长度不同的集合
            List columnSizeDiffList=compCertDB.compareColumnLength(dbLink, owner, owner2, DBConnection.getConn(ip, owner));
           //字段注释不同的集合
            List commentsDiffList=compCertDB.compColComments(dbLink, owner, owner2, DBConnection.getConn(ip, owner));


            JSONObject object = new JSONObject();
            JSONArray tableNotIn = new JSONArray(tableNotInList);
            JSONArray columnsNotIn = new JSONArray(columnsNotInList);
            JSONArray columnSizeDiff = new JSONArray(columnSizeDiffList);
            JSONArray commentsDiff = new JSONArray(commentsDiffList);

            object.put("tableNotIn",tableNotIn);
            object.put("columnsNotIn",columnsNotIn);
            object.put("columnSizeDiff",columnSizeDiff);
            object.put("commentsDiff",commentsDiff);

            ResManager.getJsonOut(response).println(JSON.toJSONString(object));

        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            ResManager.getJsonOut(response).println("{\"msg\":\""+ e.getMessage().replaceAll("\n","").replaceAll("\r","") +"\"}");
        }finally {
            ResManager.close();
        }

    }





}
